﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;

namespace ShareB.Models
{
    #region Metafield models

    //BASE DE DADOS ALTERADA NAO FOI PRECISO O PUBCONF ID NESTE MODELO essa informaçao passa para a configuraçao (lista de metafiels)
    public class MetaFieldModel
    {
        //id ddo metafield
        public int MetaFieldID {get; set;}

        //Nome do campo
        public string FieldName { get; set; }

        //Tipo do campo
        public string FieldType { get; set; }

        // Se este campo for mandatorio a todas as publicaçoes terá que ser locked para nao poder ser alterado ou apagado
        public bool Locked { get; set; }
    }

    //classe helper para a configuração de publicaçoes
    public class IncludeMetaModel : MetaFieldModel
    {
        //se este field esta incluido ou nao
        public bool Included { get; set; }
    }

    #endregion
    #region Field models
    public class FieldModel
    {
        //id deste field
        public int FieldID { get; set; }

        //id do metafield
        public int MetaFieldID { get; set; }

        //id da publicação a que esta associado
        public int PublicationID { get; set; }

        //Nome do campo
        [Required]
        [DisplayName("Field Name")]
        public string FieldName { get; set; }

        //Valor do campo
        [Required]
        [DisplayName("Field Value")]
        public string FieldValue { get; set; }
    }
    #endregion

    #region Services
    public class FieldsService
    {
        ShareBDataContext db = new ShareBDataContext();

        //Get Existing Metafields
        public IList<MetaFieldModel> GetAllMetaFields()
        {
            IList<MetaFieldModel> metaList = new List<MetaFieldModel>();

            //fetch metafields in the database
            foreach (MetaField meta in db.MetaFields)
            {
                metaList.Add(new MetaFieldModel
                {
                    FieldName = meta.FieldName,
                    FieldType = meta.FieldType,
                    MetaFieldID = meta.MetaFieldID,
                    Locked = meta.Locked
                });
            }
            return metaList;
        }

        //Get Existing Metafields to be configured as included or no
        public IList<IncludeMetaModel> GetAllFieldsToInclude()
        {
            IList<IncludeMetaModel> metaList = new List<IncludeMetaModel>();

            //fetch metafields in the database
            foreach (MetaField meta in db.MetaFields)
            {
                metaList.Add(new IncludeMetaModel
                {
                    FieldName = meta.FieldName,
                    FieldType = meta.FieldType,
                    MetaFieldID = meta.MetaFieldID,
                    Locked = meta.Locked,
                    Included = meta.Locked // se estiver locado significa que deve estra incluido em todas as configurações
                });
            }
            return metaList;
        }

        //add new meta fields
        public void CreateMeta(MetaFieldModel model)
        {
            //cria o objecto a submeter na bd
            MetaField m = new MetaField
            {
                FieldName = model.FieldName,
                FieldType = model.FieldType,
                Locked = model.Locked
            };

            db.MetaFields.InsertOnSubmit(m);
            db.SubmitChanges();
        }

        public MetaFieldModel GetMeta(int id)
        {
            //procurar na bd
            MetaField m = db.MetaFields.Single(meta => meta.MetaFieldID == id);

            if (m != null)
                return new MetaFieldModel
                {
                    FieldName = m.FieldName,
                    FieldType = m.FieldType,
                    MetaFieldID = m.MetaFieldID,
                    Locked = m.Locked
                };
            else return null;
        }


        internal void UpdateMeta(MetaFieldModel model)
        {
            MetaField m = db.MetaFields.Single(meta => meta.MetaFieldID == model.MetaFieldID);
            m.FieldName = model.FieldName;
            m.FieldType = model.FieldType;
            m.Locked = model.Locked;

            db.SubmitChanges();
        }
    }

    #endregion
}